#include "stdafx.h"
#include "UniGame.h"

//#include "tools/image/BmpReader.h"
#include "test/Test.h"

using namespace unigame;

UniGame* UniGame::s_instance = 0;

UniGame::UniGame()
{
	m_render = Render::instance();
    m_device = Device::currentDevice();
	m_stage = Stage::instance();
    m_time = Time::instance();
    m_timer = Timer::instance();
	m_input = Input::instance();
	m_lua = LuaManager::instance();
}

void UniGame::start()
{
	m_render->init();
	m_stage->init();
	
	GameObject* a1 = GameObject::create("a1");
	GameObject* a5 = GameObject::create("a5");
	GameObject* a2 = GameObject::create("a2");
	GameObject* a3 = GameObject::create("a3");
	GameObject* a4 = GameObject::create("a4");

	a1->addChild(a2);
	a1->addChild(a3);
	a2->addChild(a4);
	a3->addChild(a5);
	a1->removeChild(a2);
	m_stage->addToStage(a1);

	Camera* ca = Camera::create("ca");
	ca->transform().setLocalPosition(0, 0, -3);
	ca->transform().setLocalRotationByAngle(0, 0, 0);
	ca->fovInAngle(60);
	ca->near(0.3f);
	ca->far(1000);
	ca->size(5);
	Debug::log(ca->getCameraToWrold());
	Debug::log(ca->getProjectMatrix());
	m_stage->addToStage(ca);

	Plane p(Vector3f(-1, 0, 0), Vector3f(0, 1, 0), Vector3f(0, 0, 1));
	printf("%f\n",  p.distance());
	/*int32u id = m_stage->addDisplay();
	Display* dis = m_stage->getDisplay(id);
	dis->viewSize(0, 0, 1, 0.2f);
	Camera* ca1 = Camera::create("ca1");
	ca1->transform().setLocalPosition(0, 0, -3);
	ca1->transform().setLocalRotationByAngle(0, 0, 0);
	ca1->fovInAngle(60);
	ca1->near(0.3f);
	ca1->far(1000);
	ca1->size(5);
	
	m_stage->addToStage(ca1);
	ca1->display(dis);*/
  
	/*unixml::XmlDoc doc("abc.xml");
    TextAsset ta("abc.txt");
    int32u len = ta.getKeyValueLength();
    unsigned short* us = ta.getByKey("123");*/

	//BmpReader br(0);
	//br.read();

	update();

	/*LuaScript* ls = m_lua->loadScript("test.lua");
	ls->attachScript("test1.lua");

	int a = 0;
	int b[] = {10, 20, 50};
	char c = 0;
	std::string str;
	std::vector<char> listc;
	std::vector<short> listf;
	listf.push_back(18);
	listf.push_back(46);
	listc.push_back('a');
	listc.push_back('b');
	listc.push_back('c');
	std::map<std::string, char> mc;
	mc["abc"] = 'a';
	mc["3et"] = 's';
	std::map<std::string, int> mf;
	mf["adb"] = 23;
	mf["dfet"] = 13;
	ILuaCallback* call = uni_new ILuaCallback("test");
	ls->registerCallback(call);
	//callFunction("onMessage", "vii", "i", &list, 4, &a);
	//sendMessage("dd", 10, 5);
	ls->callFunction("onMessage", "mcmh", "mimc", &mc, &mf, &mf, &mc);
	ls->callFunction("add", "i", "i", 10, &a);
	delete call;*/
}

UniGame::~UniGame()
{
	Enum::destory();
	m_stage->destroy();
    Timer::destroy();
    Time::destroy();
	Input::destroy();
	Device::destroy();
	Render::destroy();
	LuaManager::destroy();
	while(!AutoRelease::isEmpty())
	{
		AutoRelease::clear();
	}

	Debug::outputMemoryLeak();
}

void UniGame::paint()
{
	m_render->renderStart();
	m_stage->render();
	m_render->renderEnd();
}

UniGame* UniGame::instance()
{
	if(!s_instance)
	{
		s_instance = uni_new UniGame();
	}
	return s_instance;
}

void UniGame::destroy()
{
	uni_delete(s_instance);
}

void UniGame::update()
{
	m_time->update();
    m_timer->update();
	m_stage->update();
	m_input->update();
	AutoRelease::clear();
}